<?php
namespace App\Services\ProductsService\ProductTrait;
/**
 * 产品service和产品工具类辅助trait
 *
 * 产品service和产品工具类辅助trait
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
use App\Services\PreferentialService\PreferentialService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Services\Exceptions\EntityNotFoundException;
use App\Services\Exceptions\PriceErrorException;
use App\Services\ImgServices\ImgTool;

trait product_trait
{

    /**
     * 获取产品图片
     * @param $product
     * @return mixed
     */
    public function get_product_preview_images($product)
    {
        //获取产品图片
        $previews = DB::table('product_preview_images')
            ->where(array(
                array('product_id', '=', $product->product_id),
                array('state', '=', 1)
            ))
            ->select('product_preview_original_image as original_image', 'product_preview_thumbnail_image as thumbnail')
            ->get();
        $product->original_image = array();
        $product->thumbnail = array();
        foreach ($previews as $preview) {
            $product->original_image[] = ImgTool::getProductThumbImgCDNUrlByImgPath($preview->original_image);
            $product->thumbnail[] = ImgTool::getProductThumbImgCDNUrlByImgPath($preview->thumbnail);
//            $product->original_image[] = env('IMG_URL_STD_PREFIX') . $preview->original_image;
//            $product->thumbnail[] = env('IMG_URL_STD_PREFIX') . $preview->thumbnail;
        }
        //是否有优惠 是否有赠品 获取赠品
        return $product;
    }

    /**
     * 获取定制成本
     * @param $product
     * @return array|bool
     */
    public function get_customize_price($product)
    {
        $original_price = 0;
        $preferential_price = 0;
        //模板价格 获取产品模板
        $prices = PreferentialService::getResourcePrice($product->product_temp_id, 2, $product->colour_id);
        if ($prices === false || $prices['original_price'] === false || $prices['preferential_price'] === false) {
            throw new PriceErrorException('ID为' . $product->product_temp_id . '的模板价格异常');
        }
        $original_price = bcadd($original_price, $prices['original_price'], 2);
        $preferential_price = bcadd($preferential_price, $prices['preferential_price'], 2);
        //获取素材价格
        $source_pic_ids = $this->getReSourceIds(
            array(
                array('product_id', '=', $product->product_id),
                array('type', '=', 1), array('image_type', '=', 2),
                array('state', '=', 1)
            ),
            'creative_id'
        );
        $source_pic_ids = array_unique($source_pic_ids);
        foreach ($source_pic_ids as $source_pic_id) {
            $prices = PreferentialService::getResourcePrice($source_pic_id, 1);
            if ($prices === false || $prices['original_price'] === false || $prices['preferential_price'] === false) {
                throw new PriceErrorException('ID为' . $source_pic_id . '的素材价格异常');
            }
            $original_price = bcadd($original_price, $prices['original_price'], 2);
            $preferential_price = bcadd($preferential_price, $prices['preferential_price'], 2);
        }

        //计算自定素材价格
        $custom_source_pic_ids = $this->getReSourceIds(
            array(
                array('product_id', '=', $product->product_id),
                array('type', '=', 1), array('image_type', '=', 1),
                array('state', '=', 1)
            ),
            'creative_id'
        );
        $custom_source_pic_ids = array_unique($custom_source_pic_ids);
        foreach ($custom_source_pic_ids as $source_pic_id) {
            $prices = $this->get_custom_source_pic($source_pic_id);
            $original_price = bcadd($original_price, $prices->original_price, 2);
            $preferential_price = bcadd($preferential_price, $prices->preferential_price, 2);
        }

        //计算字体价格
        $font_ids = $this->getReSourceIds(
            array(
                array('product_id', '=', $product->product_id),
                array('type', '=', 2),
                array('state', '=', 1)
            ),
            'font_id'
        );
        $font_ids = array_unique($font_ids);
        foreach ($font_ids as $font_id) {
            $prices = PreferentialService::getResourcePrice($font_id, 3);
            if ($prices === false || $prices['original_price'] === false || $prices['preferential_price'] === false) {
                throw new PriceErrorException('ID为' . $font_id . '的字体价格异常');
            }
            $original_price = bcadd($original_price, $prices['original_price'], 2);
            $preferential_price = bcadd($preferential_price, $prices['preferential_price'], 2);
        }
        return array('original_price' => $original_price, 'preferential_price' => $preferential_price);
    }

    /**
     * 获取产品价格
     * @param $product_id
     * @param int $overflow_price
     * @return array|bool
     */
    public function getTotalPrice($product)
    {
        $product_prices = $this->get_customize_price($product);
        //溢价 $overflow_price
        //总价(模板价格+素材价格+字体价格)
        $total_price = bcadd($product->overflow_price, $product_prices['original_price'], 2);
        //总的优惠价格
        $total_preferential_price = bcadd($product->overflow_price, $product_prices['preferential_price'], 2);
        return array('total_price' => $total_price, 'total_preferential_price' => $total_preferential_price);
    }

    public function getReSourceIds($where, $select)
    {
        return DB::table('product_print_materials')->where($where)->pluck($select)->toArray();
    }

    public function get_custom_source_pic($custom_source_pic_id)
    {
        $custom_source_pic = DB::table('user_creatives')
            ->where(['creative_id' => $custom_source_pic_id, 'state' => 1])
            ->select('creative_id as source_pic_id','thumbnail', 'user_id','name',
                'price as original_price','preferential_price')
            ->first();
        if (empty($custom_source_pic)) {
            throw new EntityNotFoundException('未找到ID为' . $custom_source_pic_id . '的素材');
        }
        $custom_source_pic->thumbnail = env('IMG_URL_STD_PREFIX').$custom_source_pic->thumbnail;
        $custom_source_pic->type = 1;
        // $custom_source_pic->original_price = 0;
        // $custom_source_pic->preferential_price = 0;
        $custom_source_pic->source_pic_name = $custom_source_pic->name;
        return $custom_source_pic;
    }

    /**
     * 是否存在产品
     * @param $user_id
     * @param $product_id
     * @return mixed
     */
    public function exitProducts($user_id, $product_id)
    {
        $product_count = DB::table('products')
            ->where([['product_id','=',$product_id],['user_id','=',$user_id],['state','=',1]])
            ->count();
        return $product_count;
    }
}